package cn.com.open.opensaas.answerservice.controller;

import cn.com.open.opensaas.answerservice.common.ReturnModel;
import cn.com.open.opensaas.answerservice.enums.ResultType;
import cn.com.open.opensaas.answerservice.model.question.AnswerQuestionLog;
import cn.com.open.opensaas.answerservice.model.CourseItemBank;
import cn.com.open.opensaas.answerservice.model.pager.TestPaperContent;
import cn.com.open.opensaas.answerservice.service.AnswerQuestionService;
import cn.com.open.opensaas.answerservice.service.PaperContentService;
import cn.com.open.opensaas.answerservice.util.StringUtil;
import cn.com.open.opensaas.answerservice.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 试卷判分接口
 */
@RestController
@RequestMapping("/answerquestion/")
public class AnswerQuestionController{
    private static final Logger logger = LoggerFactory.getLogger(AnswerQuestionController.class);

    @Autowired
    private PaperContentService paperContentService;

    @Autowired
    private AnswerQuestionService answerQuestionService;

    /**
     * 保存作答记录
     * @param isDecimal true  客观填空，完形填空，阅读理解按double计算分值  false 按int计算
     * @param request
     * @param response
     */

    @RequestMapping(value = "{itemBankId}/save", method = RequestMethod.POST)
    public ReturnModel save(HttpServletRequest request, HttpServletResponse response,
                            @PathVariable String itemBankId, @RequestBody AnswerQuestionLog answerQuestionLog,
                            @RequestHeader("appkey") String appkey,
                            @RequestParam(value="isDecimal", required=false) boolean isDecimal,
                            @RequestParam(value = "isHalf", required=false) boolean isHalf) {

        //非空判断
        if(StringUtil.isEmpty(itemBankId) || StringUtil.isEmpty(appkey) || StringUtils.isEmpty(answerQuestionLog) ){
            return new ReturnModel(ResultType.PARAMNULL.getPrefix(), ResultType.PARAMNULL.getDisplay());
        }

        //安全认证
        CourseItemBank courseItemBank = answerQuestionService.findCourseItemBankOne(itemBankId,appkey);
        if(StringUtils.isEmpty(courseItemBank)){
            return new ReturnModel(ResultType.SAFETYFAIL.getPrefix(), ResultType.SAFETYFAIL.getDisplay());
        }

        if (answerQuestionLog.getItems().isEmpty()) {
            return new ReturnModel(ResultType.PARAMNULL.getPrefix(), ResultType.PARAMNULL.getDisplay());
        }

        String testPaperID = answerQuestionLog.getTestPaperID();
        answerQuestionLog.setItemBankId(itemBankId);
        answerQuestionLog.setAppkey(appkey);
        answerQuestionLog.setCreateOrgID(courseItemBank.getCreateOrgID());
        answerQuestionLog.setIsHalf(isHalf);
        answerQuestionLog.setIsDecimal(isDecimal);
        if(StringUtil.isEmpty(testPaperID)){
            return answerQuestionService.save(answerQuestionLog);
        }else {
            TestPaperContent paperContent = paperContentService.findTestPaperById(testPaperID);

            if (StringUtils.isEmpty(paperContent)) {
                return new ReturnModel(ResultType.PAPERNOTFOUND.getPrefix(), ResultType.PAPERNOTFOUND.getDisplay());
            }

            if (StringUtils.isEmpty(paperContent.getContentJson())) {
                return new ReturnModel(ResultType.CONTENTISNULL.getPrefix(), ResultType.CONTENTISNULL.getDisplay());
            }


            return answerQuestionService.save(answerQuestionLog,paperContent);

        }






    }

}